<?php

/**
 * profile actions.
 *
 * @package    gofish
 * @subpackage profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class profileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      //echo $this->getUser()->getGuardUser()->getId();
      
      $group_id = $this->getUser()->getCurrentGroupId();     
      
      $this->gofish_players = Doctrine_Query::create()
                                ->select('pg.player_id, p.id, gu.username')
                                ->from('GofishPlayerGroup pg')
                                ->leftJoin('pg.GofishPlayer p')
                                ->leftJoin('p.sfGuardUser gu')
                                ->where('pg.group_id = ?',$group_id)
                                ->orderBy('p.points DESC')
                                ->execute();
                              
      /*$ids = array();
      foreach ($gofish_players as $gofish_player):
          array_push($ids, $gofish_player['id']);
      endforeach;
      
      $this->players_info = Doctrine_Query::create()
                                ->select('username')
                                ->from('sfGuardUser')
                                ->where('id IN ' . $ids . '')
                                
                          */      
  }

  public function executeRules(sfWebRequest $request)
      {
      }

  public function executeAddnew(sfWebRequest $request)
      {
          $this->addGirlForm = new GofishGirlsForm();
          $this->addGirlForm->setDefault('user_id', $this->getUser()->getGuardUser()->getId());
          $this->addGirlForm->setDefault('group_id', $this->getUser()->getCurrentGroupId());
          
          if ($request->isMethod('post'))
          {
            $this->addGirlForm->bind($request->getParameter('gofish_girls'));
            
            $points = $this->addGirlForm->getValue('point_worth');
            $bonus = $this->addGirlForm->getValue('bonus');
            $dejavu = $this->addGirlForm->getValue('dejavu');

            $this->addGirlForm->getObject()->setTotalPoints($points + $bonus - $dejavu);
            
            if ($this->addGirlForm->isValid())
            {
                $this->addGirlForm->save();
                
                $this->getUser()->setFlash('notice', sprintf('You have successfuly added ' . $this->addGirlForm->getValue('name') . ' to you list, nice work!'));
                $this->redirect('/profile/showlist');
            }
          }
      }
      
  public function executeShowlist(sfWebRequest $request)
      {
          $this->girls = Doctrine_Core::getTable('GofishGirls')->findByUserId($this->getUser()->getGuardUser()->getId());

          $this->forward404Unless($this->girls);
          
      }

  public function executeEdit(sfWebRequest $request)
  {
    $gofish_girls = Doctrine_Core::getTable('GofishGirls')->find(array($request->getParameter('id')));
    if ($gofish_girls->getUserId() != $this->getUser()->getGuardUser()->getId())
        $gofish_girls = null;
    
    $this->forward404Unless($gofish_girls, sprintf('The girl/boy does not exist or you do not have access to it.', $request->getParameter('id')));

    $this->form = new GofishGirlsForm($gofish_girls);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    
    $gofish_girls = Doctrine_Core::getTable('GofishGirls')->find(array($request->getParameter('id')));
    if ($gofish_girls->getUserId() != $this->getUser()->getGuardUser()->getId())
        $gofish_girls = null;
    
    $this->forward404Unless($gofish_girls, sprintf('The girl/boy does not exist or you do not have access to it.', $request->getParameter('id')));
    
    $this->form = new GofishGirlsForm($gofish_girls);

    $this->processForm($request, $this->form);
    

    $this->setTemplate('show');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->gofish_girls = Doctrine_Core::getTable('GofishGirls')->find(array($request->getParameter('id')));
    
    $group_id_girl = $this->gofish_girls->getGroupId();
    $group_id_user = $this->getUser()->getAttribute('group_id');
    
    if ($group_id_girl != $group_id_user)
        $this->gofish_girls = null;
    
    $this->forward404Unless($this->gofish_girls);
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($gofish_girls = Doctrine_Core::getTable('GofishGirls')->find(array($request->getParameter('id'))), sprintf('Object gofish_girls does not exist (%s).', $request->getParameter('id')));
    $gofish_girls->delete();
    
    $this->getUser()->setFlash('notice', sprintf('You have successfuly deleted ' . $gofish_girls->getName()));
    $this->redirect('profile/showlist');
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $gofish_girls = $form->save();
      
      $this->getUser()->setFlash('notice', sprintf('You have successfuly edited ' . $this->form->getObject()->getName()));
      $this->redirect('profile/show?id='.$gofish_girls->getId());
    }
  }
}
